<?php
abstract class db_abstract_model {
    private static $db_host='localhost';
    private static $db_user='root';
    private static $db_pass='';
    protected $db_name='db_name';
    protected $query;
    protected $insertid;
    protected $result_rows=array();
    private $conn;
    public $msj='Hecho';
    
    abstract protected function get();
    abstract protected function set();
    abstract protected function edit();
    abstract protected function delete();
    
    private function open_connection(){
        $this->conn=new mysqli(self::$db_host,  self::$db_user,  self::$db_pass,$this->db_name);
    }
    private function close_connection(){
        $this->conn->close();
    }
    
    protected function execute_nonquery(){
        if($_POST){
            $this->open_connection();
            $this->conn->query($this->query);
            $this->insertid=$this->conn->insert_id;
            $this->close_connection();
        }else{
            $this->$msj="Metodo no permitido";
        }
    }
    //obtiene resultados en un array asociativo
    protected function get_queryresults(){
        $this->open_connection();
        $this->result_rows=array();
        $result=$this->conn->query($this->query);
        #verifica errores de sintais Mysql
        if (!$result) {
            echo $this->query;
            throw new Exception("Database Error [{$this->conn->errno}] {$this->conn->error}");
            
        }
        
        while ($row = $result->fetch_assoc()) {
            array_push($this->result_rows, $row);
        }
        $result->free();
        $this->close_connection();
//        echo '<pre>';
//        print_r($this->result_rows);
//        echo '</pre>';
    }
    
    
  //obtiene resultados en un array numerico
    protected function get_numqueryresults(){
        $this->open_connection();
        $this->result_rows=array();
        $result=$this->conn->query($this->query);
        #verifica errores de sintais Mysql
        if (!$result) {
            throw new Exception("Database Error [{$this->conn->errno}] {$this->conn->error}");
        }
        
        while ($row = $result->fetch_row()) {
            array_push($this->result_rows, $row);
        }
        $result->free();
        $this->close_connection();
    }
}

?>
